package com.gaowl.gulimall.product.service.impl;

import com.gaowl.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.gaowl.gulimall.product.service.AttrService;
import com.gaowl.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaowl.common.utils.PageUtils;
import com.gaowl.common.utils.Query;

import com.gaowl.gulimall.product.dao.AttrGroupDao;
import com.gaowl.gulimall.product.entity.AttrGroupEntity;
import com.gaowl.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrAttrgroupRelationDao attrgroupRelationDao;

    @Autowired
    AttrService attrService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 分页查询（支持模糊查询）
     * @param params 查询参数
     * @param catelogId 类别id
     * @return 分页后的查询结果
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {


        String key = (String)params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();

        if (!StringUtils.isEmpty(key)){
            // 模糊查询
            // select * from pms_attr_group where catelog_id = ?
            // and (attr_group_id=key or attr_group_name like %key%)
            wrapper.and((obj)->{
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }

        if (catelogId == 0){   // 默认查询所有
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }else{
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    /**
     * 根据分类id，获取所有分组，及这些分组关联的属性信息
     */
    @Override
    public List<AttrGroupWithAttrsVo> findGroupInfoWithAssociatedAttrsByCatId(Long catelogId) {
        // 1.查询该类别下的分组对象
        List<AttrGroupEntity> attrGroupEntityList = this.baseMapper.selectList(
                new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVoList = attrGroupEntityList.stream().map(attrGroupEntity -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            // 2.拷贝基本信息
            BeanUtils.copyProperties(attrGroupEntity, attrGroupWithAttrsVo);
            // 3. 设置关联的所有属性对象的信息
            attrGroupWithAttrsVo.setAttrs(attrService.getRelationAttr(attrGroupEntity.getAttrGroupId()));
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());

        return attrGroupWithAttrsVoList;
    }


}